<?php

namespace org\payments;

class Mpay {

    const KEY = '82D7CE26AD6944E9B029FB371D220A21';
    const mchtId = '0000003341';

    //鉴权
    //
    public static function send_check($data) {
        $api = "http://mf.minpayment.com/mf-ams/authentication/identify";
        $param = [
            'ver' => "V3.0.3",
            'mchtId' => self::mchtId,
            'name' => $data['name'], //姓名
            'cid' => $data['cid'], //证件号码
            'token' => self::get_nonce(), //随机数
            'orderNo' => $data['orderNo'], //电商订单编号
            'type' => "1", //证件类型
            'sign' => ""
        ];


        $param['sign'] = self::getSignature($param, self::KEY);
        return self::curl_post($api, $param);
    }

    //注册
    //http://mf.minpayment.com/mf-account-gateway/customer/registCustomer
    public static function send_register($data) {
        $api = "http://mf.minpayment.com/mf-account-gateway/customer/registCustomer";
        $param = [
            'ver' => "V3.0.3",
            'mchtId' => self::mchtId,
            'documentType' => "01", //证件类型
            'identityNo' => $data['identityNo'], //证件号码
            'customNm' => $data['customNm'], //客户名称
            'customTel' => $data['customTel'], //联系电话
            'sign' => ""
        ];
        $param['sign'] = self::getSignature($param, self::KEY);
        return self::curl_post($api, $param);
    }

    //充值
    //
    public static function send_recharge($data) {
        $api = "http://mf.minpayment.com/mf-account-gateway/customer/customerRecharge";
        $param = [
            'ver' => "V3.0.3",
            'mchtId' => self::mchtId,
            'customId' => $data['customId'], //客户编号
            'rechargeAmt' => $data['rechargeAmt'], //充值金额
            'subOrderNo' => $data['subOrderNo'], //电商平台订单号
            'sign' => ""
        ];
        $param['sign'] = self::getSignature($param, self::KEY);
        return self::curl_post($api, $param);
    }

    //消费
    public static function send_dopay($data) {
        $api = "http://mf.minpayment.com/mf-account-gateway/customer/customerConsume";
        $param = [
            'ver' => "V3.0.3",
            'mchtId' => self::mchtId,
            'customId' => $data['customId'], //客户编号
            'cmAmt' => $data['cmAmt'], //消费金额
            'subOrderNo' => $data['subOrderNo'], //电商平台订单号
            'orderTime' => $data['orderTime'], //订单时间 
            'sign' => ""
        ];
        $param['sign'] = self::getSignature($param, self::KEY);
        return self::curl_post($api, $param);
    }

    private static function curl_post($url, $data) {
        $jsonStr = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
                )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return json_decode($response, true);
    }

    //获取随机数
    private static function get_nonce($length = 32) {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * 签名生成算法
     * @param  array  $params API调用的请求参数集合的关联数组，不包含sign参数
     * @param  string $secret 签名的密钥即获取access token时返回的session secret
     * @return string 返回参数签名值
     */
    private static function getSignature($params, $secret) {
        if (isset($params['sign'])) {
            unset($params['sign']);
        }
        $str = '';  //待签名字符串
        ksort($params); //先将参数以其参数名的字典序升序进行排序
        foreach ($params as $k => $v) {  //遍历排序后的参数数组中的每一个key/value对
            if ($v != "") {
                $str .= "$k=$v" . "&";  //为key/value对生成一个key=value格式的字符串，并拼接到待签名字符串后面
            }
        }
        $str .= "key=" . $secret;     //将签名密钥拼接到签名字符串最后面
        return md5($str);  //通过md5算法为签名字符串生成一个md5签名，该签名就是我们要追加的sign参数值
    }

}
